#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "api.h"

void assert_db_error_description(struct DatabaseError error, const char* expected_result)
{
    char* result = db_error_description(&error);

    assert(strcmp(expected_result, result) == 0);
}

void test_api()
{
    {
        struct DatabaseError error;
        error.tag = IsReadOnly;
        const char* expected_result = "Cannot write to read-only database";
        assert_db_error_description(error, expected_result);
    }
    {
        struct DatabaseError error;
        error.tag = IoError;
        struct IoErrorInfoDetails io_error_info_details;
        io_error_info_details.code = 1;
        struct IoErrorInfo io_error_info;
        io_error_info.details = io_error_info_details;
        error.io_error = io_error_info;
        const char* expected_result = "I/O Error, error code: 1";
        assert_db_error_description(error, expected_result);
    }
    {
        struct DatabaseError error;
        error.tag = FileCorrupted;
        struct FileCorruptedInfo file_corrupted;
        file_corrupted.code = 1;
        error.file_corrupted = file_corrupted;
        const char* expected_result = "File corrupted, run repair, error code: 1";
        assert_db_error_description(error, expected_result);
    }
}

int main(void)
{
    struct DatabaseError error;
    error.tag = IoError;
    struct IoErrorInfoDetails io_error_info_details;
    io_error_info_details.code = 1;
    struct IoErrorInfo io_error_info;
    io_error_info.details = io_error_info_details;
    error.io_error = io_error_info;
    char* result = db_error_description(&error);
    printf("(result: %s)\n",
           result);

    test_api();

    return 0;
}
